/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.EditUser;
import controller.EncryptMD5;
import entities.Users;
import java.util.Date;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@ViewScoped
public class EditAccountBean {

    private Users user;
    private String fullname;
    private String gender;
    private String phone;
    private Date dob;
    private String email;
    private String address;
    private String oldpass;
    private String newpass;
    private String retypepass;

    public Users getUser() {
        return getUserDetail();
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public String getFullname() {
        return getUserDetail().getFullname();
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getGender() {
        return getUserDetail().getGender();
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return getUserDetail().getPhone();
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getDob() {
        return getUserDetail().getBod();
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return getUserDetail().getEmail();
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return getUserDetail().getAddress();
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getOldpass() {
        return oldpass;
    }

    public void setOldpass(String oldpass) {
        this.oldpass = oldpass;
    }

    public String getNewpass() {
        return newpass;
    }

    public void setNewpass(String newpass) {
        this.newpass = newpass;
    }

    public String getRetypepass() {
        return retypepass;
    }

    public void setRetypepass(String retypepass) {
        this.retypepass = retypepass;
    }

    /**
     * Creates a new instance of EditAccountBean
     */
    public EditAccountBean() {
    }

    public Users getUserDetail() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        LoginBean lg = (LoginBean) app.createValueBinding("#{loginBean}").getValue(facesContext);
        return lg.getUser();
    }

    public String resultEditPass() {
        if (!newpass.equals(retypepass)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Retype Password invalid"));
            return "";
        }
        if (!getUserDetail().getPassword().equals(EncryptMD5.getHashString(oldpass))) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Older Password false"));
            return "";
        }
        Users u = getUserDetail();
        u.setPassword(EncryptMD5.getHashString(newpass));
        EditUser eu = new EditUser();
        boolean resultEdit = eu.resultEdit(u);
        if (resultEdit) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Action complete !"));
            return "";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Action error !"));
        return "";
    }

    public String resultEditInfo() {
        Users u = getUserDetail();
        u.setAddress(address);
        u.setBod(dob);
        u.setEmail(email);
        u.setFullname(fullname);
        u.setGender(gender);
        u.setPhone(phone);
        EditUser eu = new EditUser();
        boolean resultEdit = eu.resultEdit(u);
        if (resultEdit) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Action complete !"));
            return "";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Action error !"));
        return "";
    }
}
